package com.haierp.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.model.SellerType;
import com.haierp.service.ISellerService;
import com.haierp.service.ISellerTypeService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.SellerQueryVO;
import com.haierp.vo.SellerTypeQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;

/**
 * 
 * Title: SellerTypeController.java
 * Description: 
 * 	Seller Type Controller
 * 
 *
 * @author jc
 * Mar 17, 2017
 *
 */

@Service("sellerTypeController")
@Controller
@RequestMapping("/sellerType")
public class SellerTypeController extends BaseController {
	
	@Autowired
	private ISellerTypeService iSellerTypeService;
	
	@Autowired
	private ISellerService isellerService;
	
	@RequestMapping("/add")
	@ResponseBody
	public Object add(SellerType sellerType) {
		JsonResult<String> result = new JsonResult<>();
		ShiroUser one = this.getShiroUser();
		if (null == sellerType.getId()) {
			SellerTypeQueryVO sellerTypeQueryVO = new SellerTypeQueryVO();
			sellerTypeQueryVO.setCode(sellerType.getCode());
			//Check code is unique
			Integer count = iSellerTypeService.querySellerTypesCount(sellerTypeQueryVO);
			if (count > 0) {
				result.buildMsg("经销商类别代码不可以重复").buildIsSuccess(false);
			} else {
				sellerType.setGmtCreate(new Date());
				sellerType.setGmtModify(new Date());
				sellerType.setCompanyId(one.getCompanyId());
				result.buildIsSuccess(iSellerTypeService.insert(sellerType));
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(SellerType sellerType) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != sellerType.getId()) {
			SellerType tjSellerType = new SellerType();
			tjSellerType.setCode(sellerType.getCode());
			SellerType selSellerType = iSellerTypeService.selectOne(tjSellerType);
			if(selSellerType!=null && selSellerType.getId()!=sellerType.getId()) {
				return result.buildMsg("经销商类别代码不可以重复").buildIsSuccess(false);
			}
			
			sellerType.setGmtCreate(null);
			sellerType.setGmtModify(new Date());
			//TODO: JC, 事务或存储过程？
			boolean bIsSuccess = iSellerTypeService.updateSelectiveById(sellerType);
			
			if (bIsSuccess) {
				//update Seller table
				isellerService.updateSellerTypeByTypeCode(sellerType);
			}
			
			result.buildIsSuccess(bIsSuccess);
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(SellerType sellerType) {
		JsonResult<String> result = new JsonResult<>();

		if (null != sellerType.getId()) {
			SellerQueryVO sellerQueryVO = new SellerQueryVO();
			sellerQueryVO.setTypeId(sellerType.getId());
			
			Integer count = isellerService.querySellersCount(sellerQueryVO);
			if (count > 0) {
				result.buildMsg("经销商类别信息被使用，无法删除").buildIsSuccess(false);
			} else {
				result.buildIsSuccess(iSellerTypeService.deleteSelective(sellerType));
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<SellerType> result = new JsonResult<>();

		return result.buildData(iSellerTypeService.selectById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/querySellerTypeList")
	@ResponseBody
	public Object querySellerTypeList(SellerTypeQueryVO sellerTypeQueryVO) {
		JsonPageResult<List<SellerType>> result = iSellerTypeService.querySellerTypeList(sellerTypeQueryVO);
		return result.buildIsSuccess(true);
	}
}
